using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public class CBZ
    {
        public CBZ(char c1, char c2, bool wrongin)
        {
            this.wrongin = wrongin;
            this.c1 = c1;
            this.c2 = c2;
            regex = new Regex(string.Format(@"[\u0100-\uffff]+?\{0}[\u0100-\uffff]\{1}[\u0100-\uffff]*", c1, c2), RegexOptions.Compiled);
        }

        private bool wrongin;
        private char c1, c2;
        private Regex regex;

        public IEnumerable<string> GetStringFromText(string text)
        {
            for (Match m = regex.Match(text); m.Success; m = m.NextMatch())
            {
                yield return m.Value;
            }
        }
        public IEnumerable<KeyValuePair<string, string>> GetPairFromText(string text)
        {
            foreach (string s in GetStringFromText(text))
            {
                int i1 = s.IndexOf(c1), i2 = s.IndexOf(c2);
                if (i1 > 0 && i2 >= 0 && i2 - i1 == 2)
                {
                    string s1 = s.Substring(0, i1) + s.Substring(i2 + 1),
                           s2 = s.Substring(0, i1 - 1) + s.Substring(i1 + 1, 1) + s.Substring(i2 + 1);
                    KeyValuePair<string, string> kv;
                    if(wrongin)
                        kv = new KeyValuePair<string, string>(s1,s2);
                    else 
                        kv = new KeyValuePair<string, string>(s2,s1);
                    yield return kv;
                }
            }
        }
    }
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            char c1 = this.textBox1.Text[0], c2 = this.textBox1.Text[1];
            CBZ cbz = new CBZ(c1, c2, this.checkBox1.Checked);
            this.richTextBox2.Clear();
            Dictionary<string, object> dict = new Dictionary<string, object>();
            foreach (string s in cbz.GetStringFromText(this.richTextBox1.Text))
            {
                if(!dict.ContainsKey(s))
                {
                    dict[s] = null;
                    this.richTextBox2.AppendText(string.Format("{0}\n", s));
                }
            }
        }
    }
}